Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

470
Views
Error: Invalid hook call. | Problem to fetch data using context inside of getStaticProps (Next.js)

I'm new to react/next and I'm trying to load my post with getStaticProps, the problem is if I use the "useAppContext" or even "useContext(AppContext)"

error - Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:

  1. You might have mismatching versions of React and the renderer (such as React DOM)
  2. You might be breaking the Rules of Hooks
  3. You might have more than one copy of React in the same app

If I use this "useContext" inside of the body of my function component it indeed loads correctly but how could I retrieve the id of the post on my getStaticProps function?

My actual code:

import { GetStaticPaths, GetStaticProps } from 'next';
import { useAppContext } from '../../context/AppContext';

interface PostProps {
  post: {
    field_body: string,
    field_description: string,
    field_posts_tags: string,
    nid: string,
    title: string,
    field_readingtime: string
  }
}

export default function PostTemplate({post}: PostProps) {
  console.log(post);
  return (
    <h2>test</h2>
  )
}

export const getStaticPaths: GetStaticPaths = async() => {
  return {
    paths: [],
    fallback: true
  }
}

export const getStaticProps: GetStaticProps = async () => {
  const value = useAppContext();
  const postNid = value.postNid;
  const post = await fetch(`https://cms.jonasdev.com.br/api/posts/get/${postNid}`)
    .then(response => response.json())
    .then(response => response[0])

  return {
    props: {
      post
    },
    revalidate: 60 * 60 * 24 //24h
  }
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Since I could not find a way to use context inside of getStaticProps I changed how I handle the get request - now I'm no longer using the post id (postNid) but the slug (e.g /using-multiple-deploy-keys) to query the full data of the post. Since I'm no longer using context I was able to use getStaticProps to fetch the data.

For reference, the commits here (https://github.com/jonas-gerosa361/jonasdev/commits/main) on March 10th cover this change in detail.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!